﻿@{
    ViewBag.Title = "用户管理";
    Layout = "~/Views/Shared/_FlowForm.cshtml";
}
<script src="~/Content/scripts/plugins/jquery.md5.js"></script>
<script src="~/Content/scripts/plugins/uploadify/ajaxfileupload.js"></script>

<script>
    var keyValue = request('keyValue');
    var _code = "",_AccountType="2"; 
    $(function () {
        initControl();
    })
    //初始化控件
    function initControl() {
        //绑定组织机构
        $("#OrganizeId").ComboBoxTree({
            url: "../../BaseManage/Organize/GetTreeJsonById",
            height: "250px",
            description: "==请选择==",
        });

        $("#AccountTypeName").ComboBoxTree({
            description: "==请选择==",
            height: "200px",
            click: function (item) {
                _code = item.title;
            }
        });
      
        $('input:radio[name="AccountType"]').change(function (e) {
            _AccountType = e.currentTarget.value;
            if (_AccountType != "5") {
                $("#AccountTR").removeAttr("hidden");
                $('#OrganizeTR').attr('hidden', 'hidden');
                $("#AccountTypeName").ComboBoxTree({
                    url: "../../BaseManage/Account/GetUserTreeJson?AccountType=" + _AccountType,
                    description: "==请选择==",
                    height: "200px",
                    click: function (item) {
                        _code = item.title;
                    }
                });
            }
            else {               
                $('#AccountTR').attr('hidden', 'hidden');
                $("#OrganizeTR").removeAttr("hidden");
            }
        });
       
        $("#selectContent").append("@Html.Raw(@ViewBag.Html)");

        $('input:radio[name="EXPMode"]').change(function (e) {
            if (e.currentTarget.value == "2") {
                $(".expdateclass").removeAttr("hidden");
            }
            else {
                $(".expdateclass").attr("hidden", "hidden");
            }
        });
        //获取表单
        if (!!keyValue) {
            $.SetForm({
                url: "../../BaseManage/Account/GetFormJson",
                param: { keyValue: keyValue },
                success: function (data) {
                    $("#form1").SetWebControls(data);
                    //$("input[name='AccountType']").attr("disabled", "disabled");
                    if (data.EXPMode==2) {
                            $(".expdateclass").removeAttr("hidden");
                    }
                    if (data.AccountType == 1) {
                        //系统
                        $('#AccountTR').attr('hidden', 'hidden');
                        $('#OrganizeTR').attr('hidden', 'hidden');
                    }
                    else if(data.AccountType==5){
                        $('#AccountTR').attr('hidden', 'hidden');
                        $('#OrganizeTR').removeAttr('hidden');
                        $("#OrganizeId").ComboBoxTreeSetValue(data.OrganizeId);
                    }
                    else {
                        $('#OrganizeTR').attr('hidden', 'hidden');
                        $('#AccountTR').removeAttr('hidden');
                        $("#AccountTypeName").ComboBoxTree({
                            url: "../../BaseManage/Account/GetUserTreeJson?AccountType=" + data.AccountType,
                            description: "==请选择==",
                            height: "200px",
                            click: function (item) {
                                _code = item.title;
                            }
                        });
                        $("#AccountTypeName").ComboBoxTreeSetValue(data.SourceId);
                    }                  
                    $("input[name='AccountType'][value=" + data.AccountType + "]").attr("checked", true);
                    $("input[name='EXPMode'][value=" + data.EXPMode + "]").attr("checked", true);
                    $("#Password").val("******").attr('disabled', 'disabled');
                    if (data.HeadIcon) {     
                        document.getElementById('uploadPreview').src = top.contentPath + "/Resource/PhotoFile/" + data.Id + ".jpg";
                        
                    }
                    _code = data.Code;
                }
            });
        }

        //上传图片
        $('#uploadFile').change(function () {
            var f = document.getElementById('uploadFile').files[0]
            var src = window.URL.createObjectURL(f);
            document.getElementById('uploadPreview').src = src;
            
            //上传应用图标
            $.ajaxFileUpload({
                url: "../../BaseManage/Account/UploadFile",
                secureuri: false,
                fileElementId: 'uploadFile',
                dataType: 'json',
                success: function (data) {
                    dialogMsg(data.message, 1);
                }
            });
        });

    }

  
    //保存表单
    function AcceptClick() {
        if (!$('#form1').Validform()) {
            return false;
        }       
        var postData = $("#form1").GetWebControls(keyValue);
        var reg = /^[a-zA-Z]\w{5,17}$/
        if(!keyValue && !reg.test($.trim($("#Password").val()))){
            dialogMsg("密码以字母开头，长度在6~18位，只能包含字母、数字、下划线", 0);
            return false;
        }
        postData["Id"] = keyValue;
        postData["Name"] = $("#AccountTypeName").attr('data-text');
        postData["SourceId"] = $("#AccountTypeName").attr('data-value');
        postData["Code"] = _code;
        postData["Password"] = $.md5($.trim($("#Password").val()));
        postData["AccountType"] = $('#AccountType input[name="AccountType"]:checked ').val();
        postData["EXPMode"] = $('#EXPMode input[name="EXPMode"]:checked ').val();
        postData["CheckOnLine"] = 0;
        
        $.SaveForm({
            url: "../../BaseManage/Account/SaveForm",
            param: { "keyValue": keyValue, "strAccountEntity": JSON.stringify(postData) },
            loading: "正在保存数据...",
            success: function (data) {
                $.currentIframe().$("#gridTable").trigger("reloadGrid");
            }
        })
    }
</script>
<div style="margin-left: 10px; margin-right: 10px;">
    @*<ul class="nav nav-tabs">
        <li class="active"><a href="#BaseInfo" data-toggle="tab">基本信息</a></li>
        <li><a href="#ExpandInfo" data-toggle="tab">扩展属性</a></li>
    </ul>*@
    <div class="tab-content" style="padding-top: 15px;">
        <div id="BaseInfo" class="tab-pane active" style=" padding-right: 30px;">
            <table class="form">
                <tr>
                    <td class="formTitle">类别<font face="宋体">*</font></td>
                    <td class="formValue">
                        <div id="AccountType" class="radio">
                            <label><input name="AccountType" type="radio"  value="2" />员工</label>
                            <label> <input name="AccountType" type="radio" value="3" />客户</label>
                            <label><input name="AccountType" type="radio"  value="4" />供应商</label>
                            <label> <input name="AccountType" type="radio" checked="checked" value="5" />其他</label>
                        </div>
                    </td>                   
                </tr>
                <tr id="AccountTR" hidden>
                    <td class="formTitle">关联名称</td>
                    <td class="formValue">
                        <div id="AccountTypeName" type="selectTree" class="ui-select" isvalid="yes" checkexpession="IsScript"></div>
                    </td>
                </tr>
                <tr id="OrganizeTR" >
                    <td class="formTitle">所属机构</td>
                    <td class="formValue">
                        <div id="OrganizeId" type="selectTree" class="ui-select" isvalid="yes" checkexpession="IsScript"></div>
                    </td>
                </tr>
                <tr>
                    <td class="formTitle">账户<font face="宋体">*</font></td>
                    <td class="formValue">
                        <input id="Account" type="text" onblur="$.ExistField(this.id,'../../BaseManage/Account/ExistAccount')" class="form-control" placeholder="请输入账户名" isvalid="yes" checkexpession="NotNull" />
                    </td>                   
                </tr> 
                <tr>
                    <td class="formTitle">密码<font face="宋体">*</font></td>
                    <td class="formValue">
                        <input id="Password" type="text" class="form-control"  placeholder="请输入密码" isvalid="yes" checkexpession="NotNull" />
                    </td>
                </tr>
                <tr>
                    <th class="formTitle" style="height: 37px;">登陆系统</th>
                    <td class="formValue" id="selectContent"></td>
                </tr>
                <tr>
                    <td class="formTitle">有效期</td>
                    <td class="formValue">
                        <div id="EXPMode" class="radio">
                            <label><input name="EXPMode" type="radio" checked="checked" value="1" />永久有效</label>
                            <label> <input name="EXPMode" type="radio" value="2" />临时</label>
                        </div>
                    </td>
                </tr>
                <tr class="expdateclass" hidden>
                    <td class="formTitle">有效期限</td>
                    <td class="formValue">
                        <input id="EXPDate" type="text" class="form-control input-wdatepicker" onfocus="WdatePicker()" />
                    </td>
                </tr>
                <tr>
                    <td class="formTitle">照片</td>
                    <td>
                        <div style="margin-top: 20px; text-align: center;">
                            <div class="file" style="width: 100px; height: 100px;">
                                <img id="uploadPreview" style="width: 100px; height: 100px; border-radius: 100px;" src="~/Content/images/logo-headere47d5.png" />
                                <input type="file" name="uploadFile" id="uploadFile">
                            </div>
                            <div style="margin-top: 20px; line-height: 14px; color: #75777A; text-align: center;">
                                建议上传图片尺寸为100x100，大小不超过2M。
                            </div>
                        </div>
                    </td>
                </tr>
                @*<tr>
        <td class="formTitle">AppId</td>
        <td class="formValue">
            <input id="AppId" type="text" class="form-control" isvalid="yes" checkexpession="IsScript"/>
        </td>
    </tr>
    <tr>
        <td class="formTitle">微信Id</td>
        <td class="formValue">
            <input id="OpenId" type="text" class="form-control" isvalid="yes" checkexpession="IsScript"/>
        </td>
    </tr>*@               
                <tr>
                    <th class="formTitle" valign="top" style="padding-top: 4px;">
                        备注
                    </th>
                    <td class="formValue">
                        <textarea id="Description" class="form-control" style="height: 50px;" isvalid="yes" checkexpession="IsScript"></textarea>
                    </td>
                </tr>
            </table>
        </div>
        @*<div id="ExpandInfo" class="tab-pane ">
            <div class="app_layout app_preview" style="border-top: 1px solid #ccc;" id="frmpreview" ></div>
        </div>*@
    </div>
</div>
<style>
    .file {
        position: relative;
        display: inline-block;
        overflow: hidden;
        text-decoration: none;
        text-indent: 0;
        cursor: pointer !important;
    }

        .file input {
            position: absolute;
            font-size: 100px;
            right: 0;
            top: 0;
            opacity: 0;
            cursor: pointer !important;
        }

        .file:hover {
            text-decoration: none;
            cursor: pointer !important;
        }
</style>
